clear*
set maxvar 11000

global date _2025_10_28
cd "/Users/atri0032/Dropbox/World Bank/Temperature, poverty, and inequality/Estimations/"

**************************************************************************************************************
********************************** Figure S4: Nonlinear effects of daily temperature on poverty and inequality
**************************************************************************************************************

***************** Panel A: 3-degree bin

    // --- POOR 2.15 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin3_1 bin3_2 bin3_3 bin3_4 bin3_5 bin3_6 bin3_7 bin3_9 bin3_10 bin3_11 bin3_12 bin3_13
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor215 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)13 {
	cap gen estimate`i' = _b[bin3_`i']
	cap gen lb`i' = _b[bin3_`i'] - invttail(e(df_r),0.025)*_se[bin3_`i']
	cap gen ub`i' = _b[bin3_`i'] + invttail(e(df_r),0.025)*_se[bin3_`i']
}

gen estimate8=0
gen lb8=0
gen ub8=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=1

tempfile poor215
save `poor215'

    // --- POOR 3.65 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin3_1 bin3_2 bin3_3 bin3_4 bin3_5 bin3_6 bin3_7 bin3_9 bin3_10 bin3_11 bin3_12 bin3_13
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor365 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)13 {
	cap gen estimate`i' = _b[bin3_`i']
	cap gen lb`i' = _b[bin3_`i'] - invttail(e(df_r),0.025)*_se[bin3_`i']
	cap gen ub`i' = _b[bin3_`i'] + invttail(e(df_r),0.025)*_se[bin3_`i']
}

gen estimate8=0
gen lb8=0
gen ub8=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=2

tempfile poor365
save `poor365'

    // --- POOR 6.85 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin3_1 bin3_2 bin3_3 bin3_4 bin3_5 bin3_6 bin3_7 bin3_9 bin3_10 bin3_11 bin3_12 bin3_13
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor685 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)13 {
	cap gen estimate`i' = _b[bin3_`i']
	cap gen lb`i' = _b[bin3_`i'] - invttail(e(df_r),0.025)*_se[bin3_`i']
	cap gen ub`i' = _b[bin3_`i'] + invttail(e(df_r),0.025)*_se[bin3_`i']
}

gen estimate8=0
gen lb8=0
gen ub8=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=3

tempfile poor685
save `poor685'

    // --- Gini ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin3_1 bin3_2 bin3_3 bin3_4 bin3_5 bin3_6 bin3_7 bin3_9 bin3_10 bin3_11 bin3_12 bin3_13
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe gini `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)13 {
	cap gen estimate`i' = _b[bin3_`i']
	cap gen lb`i' = _b[bin3_`i'] - invttail(e(df_r),0.025)*_se[bin3_`i']
	cap gen ub`i' = _b[bin3_`i'] + invttail(e(df_r),0.025)*_se[bin3_`i']
}

gen estimate8=0
gen lb8=0
gen ub8=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=4

tempfile gini
save `gini'

    // --- Theil ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin3_1 bin3_2 bin3_3 bin3_4 bin3_5 bin3_6 bin3_7 bin3_9 bin3_10 bin3_11 bin3_12 bin3_13
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe theil `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)13 {
	cap gen estimate`i' = _b[bin3_`i']
	cap gen lb`i' = _b[bin3_`i'] - invttail(e(df_r),0.025)*_se[bin3_`i']
	cap gen ub`i' = _b[bin3_`i'] + invttail(e(df_r),0.025)*_se[bin3_`i']
}

gen estimate8=0
gen lb8=0
gen ub8=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=5

tempfile theil
save `theil'

    // --- Combine and plot ---

    use `poor215', clear
    append using `poor365'	
    append using `poor685'	
    append using `gini'	
    append using `theil'

    replace index = index - 0.15 if poor == 1
    replace index = index + 0.15 if poor == 3
    replace index = index + 0.15 if poor == 5

    graph twoway ///
        (rcap lb ub index if poor == 1, pstyle(ci) lcolor(navy%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 1, mcolor(navy%80)) ///
        (rcap lb ub index if poor == 2, pstyle(ci) lcolor(maroon%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 2, mcolor(maroon%80)) ///
        (rcap lb ub index if poor == 3, pstyle(ci) lcolor(forest_green%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 3, mcolor(forest_green%80)) ///
	, scheme(white_tableau) ///
	xtitle(Temperature bin) xlabel(1 "<0" 2 "[0,3)" 3 "[3,6)" 4 "[6,9)" ///
	5 "[9,12)" 6 "[12,15)" 7 "[15,18)" 8 "[18,21)" 9 "[21,24)" 10 "[24,27)" 11 "[27,30)" ///
	12 "[30,33)" 13 "33+", nogrid) ylabel(-0.15(0.05)0.15,nogrid) ///
		ytitle(Effects of temperature) yline(0, lcolor(black) lpattern(solid) lwidth(thin)) ///
		legend(row(1) order(1 "$2.15" 3 "$3.65" 5 "$6.85") pos(6)) name("gr1", replace) title(Poverty)
	
    graph twoway ///
        (rcap lb ub index if poor == 4, pstyle(ci) lcolor(navy%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 4, mcolor(navy%80)) ///
        (rcap lb ub index if poor == 5, pstyle(ci) lcolor(maroon%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 5, mcolor(maroon%80)) ///
	, scheme(white_tableau) ///
	xtitle(Temperature bin) xlabel(1 "<0" 2 "[0,3)" 3 "[3,6)" 4 "[6,9)" ///
	5 "[9,12)" 6 "[12,15)" 7 "[15,18)" 8 "[18,21)" 9 "[21,24)" 10 "[24,27)" 11 "[27,30)" ///
	12 "[30,33)" 13 "33+", nogrid) ylabel(-0.2(0.1)0.2,nogrid) ///
		ytitle(Effects of temperature) yline(0, lcolor(black) lpattern(solid) lwidth(thin)) ///
		legend(row(1) order(1 "Gini" 3 "Theil") pos(6)) name("gr2", replace) title(Inequality)
		
gr combine gr1 gr2, graphregion(fcolor(white) lcolor(white) lwidth(none) ilwidth(none) margin(zero) ) xsize(8)
			
graph export "${date}/_figures/_fig_s4a.pdf", as(pdf) replace	
		


***************** Panel B: 4-degree bin

    // --- POOR 2.15 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin4_1 bin4_2 bin4_3 bin4_4 bin4_5 bin4_7 bin4_8 bin4_9 bin4_10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor215 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bin4_`i']
	cap gen lb`i' = _b[bin4_`i'] - invttail(e(df_r),0.025)*_se[bin4_`i']
	cap gen ub`i' = _b[bin4_`i'] + invttail(e(df_r),0.025)*_se[bin4_`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=1

tempfile poor215
save `poor215'

    // --- POOR 3.65 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin4_1 bin4_2 bin4_3 bin4_4 bin4_5 bin4_7 bin4_8 bin4_9 bin4_10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor365 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bin4_`i']
	cap gen lb`i' = _b[bin4_`i'] - invttail(e(df_r),0.025)*_se[bin4_`i']
	cap gen ub`i' = _b[bin4_`i'] + invttail(e(df_r),0.025)*_se[bin4_`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=2

tempfile poor365
save `poor365'

    // --- POOR 6.85 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin4_1 bin4_2 bin4_3 bin4_4 bin4_5 bin4_7 bin4_8 bin4_9 bin4_10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor685 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bin4_`i']
	cap gen lb`i' = _b[bin4_`i'] - invttail(e(df_r),0.025)*_se[bin4_`i']
	cap gen ub`i' = _b[bin4_`i'] + invttail(e(df_r),0.025)*_se[bin4_`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=3

tempfile poor685
save `poor685'

    // --- Gini ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin4_1 bin4_2 bin4_3 bin4_4 bin4_5 bin4_7 bin4_8 bin4_9 bin4_10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe gini `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bin4_`i']
	cap gen lb`i' = _b[bin4_`i'] - invttail(e(df_r),0.025)*_se[bin4_`i']
	cap gen ub`i' = _b[bin4_`i'] + invttail(e(df_r),0.025)*_se[bin4_`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=4

tempfile gini
save `gini'

    // --- Theil ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bin4_1 bin4_2 bin4_3 bin4_4 bin4_5 bin4_7 bin4_8 bin4_9 bin4_10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe theil `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bin4_`i']
	cap gen lb`i' = _b[bin4_`i'] - invttail(e(df_r),0.025)*_se[bin4_`i']
	cap gen ub`i' = _b[bin4_`i'] + invttail(e(df_r),0.025)*_se[bin4_`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=5

tempfile theil
save `theil'

    // --- Combine and plot ---

    use `poor215', clear
    append using `poor365'	
    append using `poor685'	
    append using `gini'	
    append using `theil'

    replace index = index - 0.15 if poor == 1
    replace index = index + 0.15 if poor == 3
    replace index = index + 0.15 if poor == 5

    graph twoway ///
        (rcap lb ub index if poor == 1, pstyle(ci) lcolor(navy%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 1, mcolor(navy%80)) ///
        (rcap lb ub index if poor == 2, pstyle(ci) lcolor(maroon%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 2, mcolor(maroon%80)) ///
        (rcap lb ub index if poor == 3, pstyle(ci) lcolor(forest_green%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 3, mcolor(forest_green%80)) ///
	, scheme(white_tableau) ///
	xtitle(Temperature bin) xlabel(1 "<0" 2 "[0,4)" 3 "[4,8)" 4 "[8,12)" ///
	5 "[12,16)" 6 "[16,20)" 7 "[20,24)" 8 "[24,28)" 9 "[28,32)" 10 "32+", nogrid) ylabel(-0.06(0.03)0.12,nogrid) ///
		ytitle(Effects of temperature) yline(0, lcolor(black) lpattern(solid) lwidth(thin)) ///
		legend(row(1) order(1 "$2.15" 3 "$3.65" 5 "$6.85") pos(6)) name("gr1", replace) title(Poverty)
	
    graph twoway ///
        (rcap lb ub index if poor == 4, pstyle(ci) lcolor(navy%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 4, mcolor(navy%80)) ///
        (rcap lb ub index if poor == 5, pstyle(ci) lcolor(maroon%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 5, mcolor(maroon%80)) ///
	, scheme(white_tableau) ///
	xtitle(Temperature bin) xlabel(1 "<0" 2 "[0,4)" 3 "[4,8)" 4 "[8,12)" ///
	5 "[12,16)" 6 "[16,20)" 7 "[20,24)" 8 "[24,28)" 9 "[28,32)" 10 "32+", nogrid) ylabel(-0.06(0.03)0.12,nogrid) ///
		ytitle(Effects of temperature) yline(0, lcolor(black) lpattern(solid) lwidth(thin)) ///
		legend(row(1) order(1 "Gini" 3 "Theil") pos(6)) name("gr2", replace) title(Inequality)
		
gr combine gr1 gr2, graphregion(fcolor(white) lcolor(white) lwidth(none) ilwidth(none) margin(zero) ) xsize(8)
			
graph export "${date}/_figures/_fig_s4b.pdf", as(pdf) replace	
		
		
	
***************** Panel C: decile bin

    // --- POOR 2.15 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bind1 bind2 bind3 bind4 bind5 bind7 bind8 bind9 bind10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor215 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bind`i']
	cap gen lb`i' = _b[bind`i'] - invttail(e(df_r),0.025)*_se[bind`i']
	cap gen ub`i' = _b[bind`i'] + invttail(e(df_r),0.025)*_se[bind`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=1

tempfile poor215
save `poor215'

    // --- POOR 3.65 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bind1 bind2 bind3 bind4 bind5 bind7 bind8 bind9 bind10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor365 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bind`i']
	cap gen lb`i' = _b[bind`i'] - invttail(e(df_r),0.025)*_se[bind`i']
	cap gen ub`i' = _b[bind`i'] + invttail(e(df_r),0.025)*_se[bind`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=2

tempfile poor365
save `poor365'

    // --- POOR 6.85 ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bind1 bind2 bind3 bind4 bind5 bind7 bind8 bind9 bind10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe poor685 `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bind`i']
	cap gen lb`i' = _b[bind`i'] - invttail(e(df_r),0.025)*_se[bind`i']
	cap gen ub`i' = _b[bind`i'] + invttail(e(df_r),0.025)*_se[bind`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=3

tempfile poor685
save `poor685'
		
    // --- Gini ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bind1 bind2 bind3 bind4 bind5 bind7 bind8 bind9 bind10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe gini `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bind`i']
	cap gen lb`i' = _b[bind`i'] - invttail(e(df_r),0.025)*_se[bind`i']
	cap gen ub`i' = _b[bind`i'] + invttail(e(df_r),0.025)*_se[bind`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=4

tempfile gini
save `gini'

    // --- theil ---
use "${date}/_data/spid_for_analysis_v2.dta", clear

local temp_bin bind1 bind2 bind3 bind4 bind5 bind7 bind8 bind9 bind10
local controls ln_GDPppp Unempilo agri manuf popgr 

reghdfe theil `temp_bin' rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	

forval i=1(1)10 {
	cap gen estimate`i' = _b[bind`i']
	cap gen lb`i' = _b[bind`i'] - invttail(e(df_r),0.025)*_se[bind`i']
	cap gen ub`i' = _b[bind`i'] + invttail(e(df_r),0.025)*_se[bind`i']
}

gen estimate6=0
gen lb6=0
gen ub6=0

gen n = _n
	keep if n==1
reshape long estimate lb ub, i(n) j(index)
	keep index estimate lb ub
keep estimate lb ub index
gen poor=5

tempfile theil
save `theil'

    // --- Combine and plot ---

    use `poor215', clear
    append using `poor365'	
    append using `poor685'	
    append using `gini'	
    append using `theil'

    replace index = index - 0.15 if poor == 1
    replace index = index + 0.15 if poor == 3
    replace index = index + 0.15 if poor == 5

    graph twoway ///
        (rcap lb ub index if poor == 1, pstyle(ci) lcolor(navy%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 1, mcolor(navy%80)) ///
        (rcap lb ub index if poor == 2, pstyle(ci) lcolor(maroon%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 2, mcolor(maroon%80)) ///
        (rcap lb ub index if poor == 3, pstyle(ci) lcolor(forest_green%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 3, mcolor(forest_green%80)) ///
	, scheme(white_tableau) ///
	xtitle(Temperature bin) xlabel(1 "Bin 1" 2 "Bin 2" 3 "Bin 3" 4 "Bin 4" ///
	5 "Bin 5" 6 "Bin 6" 7 "Bin 7" 8 "Bin 8" 9 "Bin 9" 10 "Bin 10", nogrid labsize(vsmall)) ylabel(-0.06(0.03)0.12,nogrid) ///
		ytitle(Effects of temperature) yline(0, lcolor(black) lpattern(solid) lwidth(thin)) ///
		legend(row(1) order(1 "$2.15" 3 "$3.65" 5 "$6.85") pos(6)) name("gr1", replace) title(Poverty)
	
    graph twoway ///
        (rcap lb ub index if poor == 4, pstyle(ci) lcolor(navy%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 4, mcolor(navy%80)) ///
        (rcap lb ub index if poor == 5, pstyle(ci) lcolor(maroon%80) msize(medium) lwidth(medium)) ///
        (scatter estimate index if poor == 5, mcolor(maroon%80)) ///
	, scheme(white_tableau) ///
	xtitle(Temperature bin) xlabel(1 "Bin 1" 2 "Bin 2" 3 "Bin 3" 4 "Bin 4" ///
	5 "Bin 5" 6 "Bin 6" 7 "Bin 7" 8 "Bin 8" 9 "Bin 9" 10 "Bin 10", nogrid labsize(vsmall)) ylabel(-0.06(0.03)0.12,nogrid) ///
		ytitle(Effects of temperature) yline(0, lcolor(black) lpattern(solid) lwidth(thin)) ///
		legend(row(1) order(1 "Gini" 3 "Theil") pos(6)) name("gr2", replace) title(Inequality)
		
gr combine gr1 gr2, graphregion(fcolor(white) lcolor(white) lwidth(none) ilwidth(none) margin(zero) ) xsize(8)
			
graph export "${date}/_figures/_fig_s4c.pdf", as(pdf) replace	
		
		